package br.com.webprojeto.colecoes;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.webprojeto.basicas.*;



public class ColecaoEndereco {

	public void adicionarEndereco(Endereco E) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		sessao.beginTransaction();
		sessao.saveOrUpdate(E);
		sessao.getTransaction().commit();
	}
	 
	public void removerEndereco(long codEndereco) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		sessao.beginTransaction();
		Endereco E = (Endereco) sessao.load(Endereco.class, codEndereco);
		sessao.delete(E);
		sessao.getTransaction().commit();
	}
	 
	public void atualizarEndereco(Endereco E) {
	 	Session sessao = HibernateUtil.getSessionFactory().openSession();
		sessao.beginTransaction();
		sessao.update(E);
		sessao.getTransaction().commit();
		
	}
	 
	public List<Endereco> listarEndereco() {

		Session sessao = HibernateUtil.getSessionFactory().openSession();
		sessao.beginTransaction();
		Query consulta = sessao.createQuery("from Endereco");
		return consulta.list();

		
	}
	 
	public Endereco procurarEndereco(String nomeRua) {
		
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		sessao.beginTransaction();
		Query consulta = sessao.createQuery("from Endereco where nomeRua like ?");
		consulta.setParameter(1, nomeRua);
		Endereco E = (Endereco) sessao.load(Endereco.class, nomeRua);
		sessao.getTransaction().commit();
		return E;
		
		
	}
}
